package com.yanqu.road.server.cmd.activity.biaoju;

import com.yanqu.road.entity.activity.biaoju.UserBiaoJuData;
import com.yanqu.road.logic.activity.biaoju.BiaoJuConfig;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.BiaoJuProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.biaoju.ActivityBiaoJuModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.biaoju.BiaoJuConfigMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_BIAOJU_CITY_BUILD, desc = "城市建设")
public class BiaoJuCityBuildCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        BiaoJuProto.BiaoJuCityBuildReqMsg reqMsg = BiaoJuProto.BiaoJuCityBuildReqMsg.parseFrom(packet.getBytes());

        BiaoJuConfig config = BiaoJuConfigMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo()) || reqMsg.getActivityId() != config.getActivityId()) {
            ret = GameErrorCode.E_ACTIVITY_NOT_OPEN;
            return;
        }

        if (!config.isInDayOpenTime()) {
            ret = GameErrorCode.E_ACTIVITY_NOT_OPEN;
            return;
        }

        int groupId = GameUnionActivityGroupMgr.getGroupId(config.getActivityId(), player.getUserId());
        if (groupId == UnionActivityGroupHelper.NO_GROUP_ID) {
            ret = GameErrorCode.E_BIAOJU_NOT_ABLE;
            return;
        }

        UserBiaoJuData userData = player.getModule(ActivityBiaoJuModule.class).getUserData();
        if (userData == null) {
            ret = GameErrorCode.E_BIAOJU_NOT_ABLE;
            return;
        }

        ret = player.getModule(ActivityBiaoJuModule.class).buildCity(reqMsg);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret > 0) {
            //获得注解的code
            int code = this.getClass().getAnnotation(Cmd.class).code();
            int backClientProtocol = code % 10000;
            player.sendPacket(backClientProtocol, BiaoJuProto.BiaoJuMainSceneRespMsg.newBuilder().setRet(ret));
        }
    }

}
